/**
 * @author Tobias Boehm
 *
 * 10.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.client.ClientChatmessageMessage;
import org.anddev.andengine.extension.tbsengine.player.BaseHumanPlayer;
import org.anddev.andengine.extension.tbsengine.player.BasePlayer;

/**
 * This server message should be used to forward incoming {@link ClientChatmessageMessage}
 * to its recipients.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerChatMessageForwardMessage extends SecureServerMessage implements DefaultTBSserverMessageFlags, ICopyable<ServerChatMessageForwardMessage> {

	private UUID mSendingPlayerID = null;
	private String mSendingPlayerName = "";
	private String mMessage = "";
	private long mTimeStamp = -1;
	
	public ServerChatMessageForwardMessage(){
		
	}
	
	public ServerChatMessageForwardMessage(final BasePlayer pPlayer, final String pMessage){
		
		this.mSendingPlayerID = pPlayer.getPlayerID();
		
		this.mMessage = pMessage;
		
		this.mTimeStamp = System.currentTimeMillis();
	}
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_CHAT_MESSAGE_FORWARD;
	}
	
	@Override
	protected void onReadDecryptedTransmissionData(
			DataInputStream pDataInputStream) throws IOException {

		this.mSendingPlayerID = UUID.fromString(pDataInputStream.readUTF());
		
		this.mSendingPlayerName = pDataInputStream.readUTF();
		
		this.mMessage = pDataInputStream.readUTF();
		
		this.mTimeStamp = pDataInputStream.readLong();
	}
	
	@Override
	protected void onWriteEncryptedTransmissionData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeUTF(this.mSendingPlayerID.toString());
		
		pDataOutputStream.writeUTF(this.mSendingPlayerName);
		
		pDataOutputStream.writeUTF(this.mMessage);
		
		pDataOutputStream.writeLong(this.mTimeStamp);
	}

	/**
	 * @return the mSendingPlayerID
	 */
	public UUID getSendingPlayerID() {
		return mSendingPlayerID;
	}
	

	/**
	 * @return the mSendingPlayerName
	 */
	public String getSendingPlayerName() {
		return mSendingPlayerName;
	}

	/**
	 * @param mSendingPlayerID the mSendingPlayerID to set
	 */
	public void setSendingPlayer(final BaseHumanPlayer pSendingPlayer) {
		this.mSendingPlayerID = pSendingPlayer.getPlayerID();
		this.mSendingPlayerName = pSendingPlayer.getName();
	}

	/**
	 * @return the mMessage
	 */
	public String getMessage() {
		return mMessage;
	}

	/**
	 * @param mMessage the mMessage to set
	 */
	public void setMessage(String mMessage) {
		this.mMessage = mMessage;
	}

	/**
	 * @return the mTimeStamp
	 */
	public long getTimeStamp() {
		return mTimeStamp;
	}

	/**
	 * @param mTimeStamp the mTimeStamp to set
	 */
	public void setTimeStamp(long mTimeStamp) {
		this.mTimeStamp = mTimeStamp;
	}

	@Override
	public ServerChatMessageForwardMessage copy() {
		
		final ServerChatMessageForwardMessage copy = new ServerChatMessageForwardMessage();
		
		copy.mMessage = this.mMessage;
		copy.mSendingPlayerID = this.mSendingPlayerID;
		copy.mTimeStamp = this.mTimeStamp;
		copy.mSendingPlayerName = this.mSendingPlayerName;
		
		return copy;	
	}


}
